<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the Session_Store_Memcache class.
 *
 * PHP version 5
 *
 * This file is part of Majic - A rapid-development web application framework
 * for PHP 5.
 *
 * Majic is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * Majic is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Majic. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     Session
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2007-2008 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: Memcache.php 34 2008-07-19 04:50:53Z mjijackson $
 */

/**
 * Enables storing of session data using
 * {@link http://www.danga.com/memcached/ Memcache}. Requires the PECL
 * {@link http://pecl.php.net/package/memcache memcache} package.
 *
 * @package     Session
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
class Session_Store_Memcache extends Session_Store
{

    /**
     * This store's Memcache object.
     *
     * @var     Memcache
     * @access  private
     */
    private $mc;

    /**
     * A prefix that will be prepended to all session keys to avoid collision
     * with other Memcache keys. Defaults to "session" plus the host name.
     *
     * @var     string
     * @access  private
     */
    private $prefix;

    /**
     * Constructor.
     *
     * @param   Memcache    $mc     The Memcache object to use for this store
     * @access  public
     */
    public function __construct(Memcache $mc)
    {
        $this->mc = $mc;
        $this->prefix = 'session-' . getenv('HOSTNAME') . ':';
    }

    /**
     * @see     Session_Store::open()
     */
    public function open($save_path, $name)
    {
        return true;
    }

    /**
     * @see     Session_Store::close()
     */
    public function close()
    {
        return true;
    }

    /**
     * @see     Session_Store::read()
     */
    public function read($id)
    {
        $data = $this->mc->get($this->prefix . $id);
        return $data ? $data : '';
    }

    /**
     * @see     Session_Store::write()
     */
    public function write($id, $data)
    {
        return $this->mc->set($this->prefix . $id, $data, MEMCACHE_COMPRESSED,
            time() + ini_get('session.gc_maxlifetime'));
    }

    /**
     * @see     Session_Store::destroy()
     */
    public function destroy($id)
    {
        return $this->mc->delete($this->prefix . $id, 0);
    }

    /**
     * @see     Session_Store::gc()
     */
    public function gc($maxlifetime)
    {
        return true;
    }

    /**
     * Sets the {@link $prefix prefix} that will be used for all session keys.
     *
     * @param   string  $prefix         The prefix to use
     * @return  void
     * @access  public
     */
    public function setPrefix($prefix)
    {
        $this->prefix = $prefix;
    }

}

?>
